package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.SearchTechnologyAdapter;
import com.digitalchina.lxnsy.api.BusinessTechnology;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.TechnologyVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.UserXML;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/1/13 17:09
 * 修改备注：
 * ================================================
 */

public class TechnologyAskActivity extends BaseActivity {

    @Bind(R.id.search_ask_lv)
    ListView searchAskLv;
    @Bind(R.id.search_ask_et)
    EditText searchAskEt;

    private SearchTechnologyAdapter adapter;
    private List<TechnologyVO.QuestionBean> questionVOList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_technology_ask);
        setTitle("提问");
        setRightText("下一步");
    }

    @Override
    public void initMember() {

        adapter = new SearchTechnologyAdapter(this, questionVOList);
        searchAskLv.setAdapter(adapter);

    }

    @Override
    public void addListener() {

        searchAskEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    BusinessTechnology.questionAuto(TechnologyAskActivity.this, s.toString(), "5", mHandler);
                } else {
                    adapter.clearListData();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        searchAskLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TechnologyVO technologyVO = new TechnologyVO();
                TechnologyVO.PersonBeanX personBeanX = new TechnologyVO.PersonBeanX();
                personBeanX.setUserAccId(UserXML.getUserId(TechnologyAskActivity.this));
                technologyVO.setQuestion(adapter.getListData().get(position));
                technologyVO.setPerson(personBeanX);
                GotoUtil.gotoActivity(TechnologyAskActivity.this, AnswerListActivity.class, "TechnologyVO", technologyVO);
            }
        });

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_QUESTION_AUTO_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_QUESTION_AUTO_FIELD:
                        LogUtils.e(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        questionVOList = FastJsonUtil.getListBean(object.toString(), "body", "questions", TechnologyVO.QuestionBean.class);


        if (questionVOList != null) {
            adapter.setListData(questionVOList);
        }
    }

    @Override
    public void rightOnClick() {

        if (StringUtil.isStrEmpty(searchAskEt.getText().toString())) {
            showToast("请输入您的问题");
            return;
        }

        if (StringUtil.lastCharEqu(searchAskEt.getText().toString(), '?') || StringUtil.lastCharEqu(searchAskEt.getText().toString(), '？')) {
            GotoUtil.gotoActivity(TechnologyAskActivity.this, EditHtmlActivity.class, "title", searchAskEt.getText().toString());
        } else

            showToast("提问必须以问号结束");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
